<!--
 * @Description:退款管理
-->
<template>
  <div class="registered-container refund appMainWarp">
    <el-form :inline="true" :model="searchKey" class="demo-form-inline">
      <el-form-item label="自助机订单号:">
        <el-input
          v-model="searchKey.order_no"
          placeholder="请输入自助机订单号"
          @change="suborder_no"
        />
      </el-form-item>

      <el-form-item label="支付方式:">
        <el-select v-model="searchKey.pay_style" placeholder="请选择">
          <el-option
            v-for="item in searchKey.PayStyle"
            :key="item.no"
            :label="item.name"
            :value="item.no"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="交易时间:">
        <el-date-picker
          v-model="searchKey.time"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
          :picker-options="pickerOptions"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="onLoad"
        >查询</el-button>
      </el-form-item>
    </el-form>

    <template>
      <el-table
        ref="table"
        v-loading="page.tableLoading"
        v-el-height-adaptive-table="{ bottomOffset: 90 }"
        max-height="500px"
        center
        class="table"
        :data="searchDate"
        stripe
        border
      >
        <el-table-column label="订单id" align="center">
          <template slot-scope="scope">{{ scope.row.id || "-" }}</template>
        </el-table-column>
        <el-table-column label="平台(自助机)订单号" align="center">
          <template slot-scope="scope">{{
            scope.row.self_order_no || "-"
          }}</template>
        </el-table-column>
        <el-table-column label="第三方支付系统订单号" align="center">
          <template slot-scope="scope">{{
            scope.row.trade_no || "-"
          }}</template>
        </el-table-column>
        <el-table-column label="支付金额" align="center">
          <template slot-scope="scope">{{ scope.row.pay_fee || "-" }}</template>
        </el-table-column>
        <el-table-column label="支付渠道" align="center">
          <template slot-scope="scope">{{
            scope.row.pay_type_name || "-"
          }}</template>
        </el-table-column>
        <el-table-column label="是否可退款" align="center">
          <template slot-scope="scope">{{
            scope.row.flag == 1 ? "否" : scope.row.flag == 2 ? "是" : "-"
          }}</template>
        </el-table-column>
        <el-table-column label="可退款金额" align="center">
          <template slot-scope="scope">{{
            scope.row.refund_amount || "-"
          }}</template>
        </el-table-column>
        <!-- <el-table-column label="患者姓名" align="center">
          <template slot-scope="scope">{{ scope.row.patient_name||'0' }}</template>
        </el-table-column> -->
        <el-table-column label="订单信息" align="center">
          <template slot-scope="scope">{{ scope.row.info || "0" }}</template>
        </el-table-column>
        <el-table-column label="交易时间" align="center">
          <template slot-scope="scope">{{
            scope.row.created_time || "0"
          }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="props">
            <el-link
              v-if="props.row.flag == 2"
              size="mini"
              :underline="false"
              type="primary"
              @click="refundBtn(props.row)"
            >&nbsp;&nbsp;&nbsp;退款&nbsp;&nbsp;&nbsp;</el-link>
            <span v-else>不可退款原因：{{ props.row.desc }}</span>
          </template>
        </el-table-column>
      </el-table>
    </template>

    <pagination
      :total="page.totalRow"
      :page.sync="page.currentPage"
      :limit.sync="page.pageSize"
      @pagination="onSearch"
    />
  </div>
</template>

<script>
import elHeightAdaptiveTable from '@/directive/el-table'
import { getConfigType } from '@/api/transactRecords/index'
import { todayrefundlist } from '@/api/realTimeToAccount/index'
import { refundMang } from '@/api/refundManagement/index'
import Pagination from '@/components/Pagination' // 分页
export default {
  name: 'RealTime',
  components: {
    Pagination
  },
  directives: { elHeightAdaptiveTable },

  data() {
    return {
      quaflat: 0, // 按钮默认
      date: 1, // 按天月年
      searchKey: {
        order_no: '', // 订单号
        time: '', // 账单日期
        pay_style: '', // 支付方式
        PayStyle: []
      },

      searchDate: [{}],

      name: '',
      status: 1,
      refundcont: {},
      addModel: {
        // 退款弹窗
        flag: false,
        item: {}
      },

      page: {
        tableLoading: false,
        currentPage: 1, // 当前页
        pageSize: 10, // 默认条数
        totalRow: 0, // 总条数
        totalPage: 0 // 总页数
      },
      pickerOptions: {
        // 日期不能点击
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      }
    }
  },

  mounted() {
    this.getConfig()
    this.onLoad()
  },
  methods: {
    expandChange(row) {
      var that = this
      row.rowFlag = !row.rowFlag
      if (row.rowFlag) {
        that.$nextTick(() => {
          that.$refs.table.toggleRowExpansion(row, true) // 加载完数据展开行
        })
      } else {
        this.$refs.table.toggleRowExpansion(row, false) // 关闭行
      }
    },
    // 系统配置搜索
    getConfig() {
      var that = this
      var params = {}
      getConfigType(params)
        .then((res) => {
          var defaultValue = [{ id: '', no: '', name: '全部' }]
          // 支付方式
          var tempPay = res.data.PayStyle
          var PayStyle = [...defaultValue, ...tempPay]
          that.searchKey.PayStyle = PayStyle
        })
        .catch(() => {})
    },
    // 查询列表
    onLoad() {
      this.page.currentPage = 1
      this.onSearch()
    },
    onSearch() {
      var params = {}
      var that = this
      params.page = that.page.currentPage // 当前页数
      params.limit = that.page.pageSize // 每页展示条数
      // if (that.searchKey.time && that.searchKey.time.length > 0) {
      //   params.start_time = that.searchKey.time[0]
      //   params.end_time = that.searchKey.time[1]
      // } else {
      //   params.start_time = ''
      //   params.end_time = ''
      // }
      params.start_time = that.searchKey.time
      params.end_time = that.searchKey.time
      params.trade_no = that.searchKey.order_no
      params.pay_type = that.searchKey.pay_style
      that.page.tableLoading = true
      todayrefundlist(params)
        .then((res) => {
          that.page.tableLoading = false
          that.searchDate = res.data
          // that.page.totalPage = res.data.pageSum
          // that.page.totalRow = parseInt(res.data.count)
        })
        .catch(() => {
          that.page.tableLoading = true
        })
    },
    // 退款
    refundBtn(item) {
      this.$confirm('确定要退款吗？退款后不可恢复！', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          var params = item
          var that = this
          that.page.tableLoading = true
          refundMang(params)
            .then((res) => {
              that.page.tableLoading = false
              if (res.data.code === 1) {
                that.refundcont = res.data.refund_status
                that.$message({
                  type: 'success',
                  message: res.data.msg
                })
                that.onSearch()
              } else {
                that.$message({
                  type: 'warning',
                  message: res.data.msg
                })
              }
            })
            .catch(() => {
              that.page.tableLoading = false
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
    },
    // 验证订单号
    suborder_no() {
      const id = this.searchKey.order_no
      const reg = /^[0-9a-zA-Z]+$/
      if (!reg.test(id)) {
        this.$message({
          type: 'warning',
          message: '请输入正确的订单号'
        })
        this.searchKey.order_no = ''
        return false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.refund {
  position: relative;
  width: 100%;
  .demo-form-inline {
    .el-button--primary {
      background-color: #579ef8 !important;
      color: #fff !important;
    }
    .el-button {
      background-color: #e8f4ff;
      color: #579ef8;
      border: 1px solid rgba(87, 158, 248, 1);
    }
  }
}
.el-form-item__content .orderItem {
  display: inline-block;
  vertical-align: top;
  width: 290px;
  margin-right: 20px;
}
.el-form-item__content .orderItem span {
  font-weight: bold;
}
ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
.details {
  text-align: left;
}
</style>
<style >
.el-table__expand-icon {
  display: none;
}
.el-table__expanded-cell[class*="cell"] {
  padding: 10px 0;
}
</style>

